﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Bidder_CompanyGMPUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCompany);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumComapanyRG);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                System.Data.IDataReader dr;
                string strSql = "select isnull(checkstatus,0) as checkstatus from company where Accountid='" + _Account.AccountId.ToString() + "'";
                Db db = new Db();
                dr = db.DataReader(strSql);
                while (dr.Read())
                {
                    if (dr["checkstatus"].ToString().Equals("1") || dr["checkstatus"].ToString().Equals("3")) BtnSave.Visible = false;
                    else BtnSave.Visible = true;
                }
                dr.Close();
                db.Close();

                string   Id = SysFun.IsSQL(MsgBox.JieMi_Decrypt(Request.QueryString["ID"]));
                
                if (SysFun.ToLong(MsgBox.JieMi_Decrypt(Request.QueryString["AccountId"])) == _Account.AccountId)
                {
                    if (!IsPostBack)
                    {
                        db = new Db(); 
                        strSql = "select * from companygmp where id=" + Id;
                        dr = db.DataReader(strSql);
                        while (dr.Read())
                        {
                            txtAccountId.Text = SysFun.ToTrim(dr["accountid"]);
                            txtAccountId.Visible = false;
                            txtGmpCode.Text = SysFun.ToTrim(dr["gmpcode"]);
                            txtGmpEndDate.Text = SysFun.ToTrim(dr["gmpenddate"]);
                            txtGmpRange.Text = SysFun.ToTrim(dr["gmprange"]);
                            txtGmpStarDate.Text = SysFun.ToTrim(dr["gmpstardate"]);
                        }
                        dr.Close();
                        dr.Dispose();
                        db.Close();
                    }
                }
                else
                {
                    Response.Write("<script language=JavaScript>alert('您无权修改别的的企业GMP信息！！！');</script>");
                    Response.Write("<script>window.close();</script>");
                }
            }
            else
            {
                Response.Write("<script language=JavaScript>alert('您无权添加企业GMP信息！！！');</script>");
                Response.Write("<script>window.close();</script>");
            }
        }

    }
    protected void BtnSave_Click(object sender, EventArgs e)
    {
        string Id, AccountId = "", GMPCode, GMPSatrDate, GMPEndDate, GMPRange;
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        if (_Account != null)
        {
            Id = SysFun.IsSQL(SysFun.ToTrim(MsgBox.JieMi_Decrypt(Request.QueryString["ID"])));
            Db db1 = new Db();
            System.Data.IDataReader dr;
            string strSql = "select accountid  from dbo.companygmp where id=" + Id;
            dr = db1.DataReader(strSql);
            if (dr.Read())
            {
                AccountId = SysFun.ToTrim(dr["accountid"]);
            }
            dr.Close();
            dr.Dispose();
            db1.Close();
            if (AccountId != SysFun.ToTrim(_Account.AccountId))
            {
                Response.Write("<script language=JavaScript>alert('你无权修改该GMP信息。请核实身份后再做修改！！！');window.close();</script>");
            }
            else
            {
                AccountId = SysFun.ToTrim(txtAccountId.Text);
                GMPCode = SysFun.ToTrim(txtGmpCode.Text);
                GMPEndDate = SysFun.ToTrim(SysFun.ToDateTime(txtGmpEndDate.Text).ToString());
                GMPRange = SysFun.ToTrim(txtGmpRange.Text);
                GMPSatrDate = SysFun.ToTrim(SysFun.ToDateTime(txtGmpStarDate.Text).ToString());

                int rowCun = 0;
                string sql = "select count(id) as cun from companygmp where gmpcode='" + GMPCode + "'and id<>'" + Id + "' ";
                Db db2 = new Db();
                System.Data.IDataReader dr2;
                dr2 = db2.DataReader(sql);
                if (dr2.Read())
                {
                    rowCun = SysFun.ToInt(dr2["cun"]);
                }
                dr2.Close();
                dr2.Dispose();
                db2.Close();
                if (rowCun > 0)
                {
                    Response.Write("<script language=JavaScript>alert('该GMP在数据库中已经存在！！！');</script>");
                    return;
                }
                else
                {

                    string strSql1 = "update   companygmp set accountid='" + AccountId + "',gmpcode='" + GMPCode + "',gmpstardate='" + GMPSatrDate
                        + "',gmpenddate='" + GMPEndDate + "',gmprange='" + GMPRange + "' where id=" + Id;
                    Db db = new Db();
                    db.Command(strSql1);
                    db.Close();
                    Response.Write("<script>alert('保存成功！！！');</script>");
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "更新", "companygmp", strSql1, "../Bidder/CompanyGMPUpdate.aspx");
                }
            }
        }
        else
        {
            Response.Write("<script language=JavaScript>alert('您无权修改企业GMP信息！！！');</script>");
        }
        Response.Write("<script>window.opener.location.reload();window.close();</script>");

    }
}
